package com.zenika.zenbinding.client.handler;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.model.ValueModel;

public class TextBoxHandler extends ValueBindingHandler implements ChangeHandler {
  
  private ValueModel model;
  private TextBox textBox;
  
  public TextBoxHandler(TextBox textBox, ValueModel model) {
    super();
    this.textBox = textBox;
    this.model = model;
    this.model.setHandler(this);
    this.bind();
  }
  
  public Object getValue() {
    return this.textBox.getText();
  }
  
  public Widget getWidget() {
    return this.textBox;
  }
  
  public void onChange(ChangeEvent event) {
    this.fireChange(this.textBox.getText(), this);
  }
  
  public void setErrorStyle() {
    this.textBox.setStyleName("error-TextBox");
  }
  
  public void setRegularStyle() {
    this.textBox.setStyleName("gwt-TextBox");
  }
  
  private void bind() {
    this.textBox.setText(this.model.getValue());
    this.textBox.addChangeHandler(this);
  }
  
}
